package com.tanhua.manage.msg;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.manage.enums.LogTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 向MQ发送消息  不处理 之后再统一处理
 * @author mxk
 * @date 2021/3/29 10:23
 */
@Component
@RocketMQMessageListener(topic = "tanhua-sso-login",
        consumerGroup = "tanhua-sso-login-consumer")
@Slf4j
public class LoginMsgConsumer implements RocketMQListener<String> {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    private static final ObjectMapper MAPPER=new ObjectMapper();
    @Override
    public void onMessage(String msg) {
        try {
            Map<String,Object> hashMap = MAPPER.readValue(msg, HashMap.class);
            if (hashMap.get("id")!=null){
                hashMap.put("userId",hashMap.get("id"));
            }
            hashMap.put("type", LogTypeEnum.LOGIN.getValue());
            log.info("收到用户登录的信息,将其转到 tanhua-log topic");
            //将消息发送到MQ
            rocketMQTemplate.convertAndSend("tanhua-log",hashMap);
        } catch (IOException e) {
            log.error("消息处理失败",e);
        }
    }
}
